import CryptoJS from "crypto-js";
// import { post } from "../api/axios";
import axios from "axios";

// 获取链接参数
export function getQuerystring(key) {
  const url = new URL(window.location.href);
  return url.searchParams.get(key);
}
export function getRouteQuery(vm, key) {
  const params = vm.$route.query;
  return params[key];
}
export function getQuery(vm, key) {
  return vm.$route
    ? getQuerystring(key) || getRouteQuery(vm, key) || ''
    : getQuerystring(key) || '';
}

/*
 * AES加密信息
 */
const generateAesConfig = (system = "webox") => {
  const weboxIv = "0CoqNNJUmKmUJglo";
  const weboxKey = "CUHRGUq1aCipQiKg";
  // 十六位十六进制数作为密钥
  let key = "";
  // 十六位十六进制数作为密钥偏移量
  let iv = "";
  switch (system) {
    case "crm": {
      key = CryptoJS.enc.Utf8.parse(crmKey);
      iv = CryptoJS.enc.Utf8.parse(crmIv);
      break;
    }
    case "webox":
    default: {
      key = CryptoJS.enc.Utf8.parse(weboxKey);
      iv = CryptoJS.enc.Utf8.parse(weboxIv);
      break;
    }
  }
  const aesConfig = {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  };
  const data = { key, aesConfig };
  return data;
};
export const aesEncrypt = (word, system = "webox") => {
  const { key, aesConfig } = generateAesConfig(system);
  if (word) {
    const encodedString = CryptoJS.enc.Utf8.parse(word);
    const encryptedResults = CryptoJS.AES.encrypt(
      encodedString,
      key,
      aesConfig
    );
    const result = encryptedResults.ciphertext.toString();
    return result;
  } else {
    return "";
  }
};

/*
 * AES解密信息
 */
export const aesDecrypt = (word, system = "webox") => {
  const { key, aesConfig } = generateAesConfig(system);
  const encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  const encodedString = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  const decryptedResults = CryptoJS.AES.decrypt(encodedString, key, aesConfig);
  const result = String(decryptedResults.toString(CryptoJS.enc.Utf8));
  return result;
};

// 获取请求头参数
export const getHeadersParams = (bizType=1) => {
  let serverId = '';
  let secret = '';
  switch (bizType) {
    case 1:
    case 3:
    default: {
      serverId = process.env.VUE_APP_JJ_WEBOX_SERVER_ID;
      secret = process.env.VUE_APP_JJ_WEBOX_SECRET;
      break;
    }
    case 2: {
      serverId = process.env.VUE_APP_TJ_WEBOX_SERVER_ID;
      secret = process.env.VUE_APP_TJ_WEBOX_SECRET;
      break;
    }
  }
  return { serverId, secret };
};

/*
 * 验签
 */
export const getSign = (data, vm,bizType) => {
  const { secret } = getHeadersParams(bizType);
  var newKey = Object.keys(data).sort();
  let obj2 = {};
  newKey.forEach((v, i) => {
    obj2[v] = data[v];
  });
  let str = JSON.stringify(obj2) + secret;
  return vm.$md5(str);
};

export function getAllParams(vm) {
  const url = new URL(window.location.href);
  const urlParams = url.searchParams;
  let paramsObj = {};
  for (const [key, value] of urlParams.entries()) {
    paramsObj[key] = value;
  }
  if (vm.$route) {
    const routeQueryObj = vm.$route.query;
    paramsObj = Object.assign(paramsObj, routeQueryObj);
  }
  return paramsObj;
}

//获取bizType
// ${getBaseUrl(this.bizType)}
export const getBizType = bid =>
  new Promise((reslove, reject) => {
    axios.get(`https://test-data-platform.qh5800.com/api/1/qq/group/bizType?bid=${bid}`)
      .then(res => {
        reslove(res.data.data.bizType);
      }).catch(err => {
        reject('程序开小差了，请稍后再试');
        console.log(err);
      })
  })

/* 
 * 获取域名部分
 */
export function getBaseUrl(bizType) {
  const protocol = window.location.protocol;
  const baseUrl = {
    jj: `${protocol}${process.env.VUE_APP_JJ_URL}`,
    tj: `${protocol}${process.env.VUE_APP_TJ_URL}`,
    article: `${protocol}${process.env.VUE_APP_ARTICLE_API_HOST}`,
    bizType: `${protocol}${process.env.VUE_APP_GETBIZTYPE_URL}`,
    yskj: `${protocol}${process.env.VUE_APP_YS_PLATFORM_API_HOST}`,
  }
  let url = ''
  if (bizType == 1 || bizType == 3) {
    url = baseUrl.jj
  return url

  } else if (bizType == 2) {
    url = baseUrl.tj
  return url
  }
  return baseUrl
}

